package licode.unisop.realm.application.service;

import cn.hutool.json.JSONUtil;
import licode.unisop.provider.api.SiAuthenticationProvider;
import licode.unisop.provider.info.SiIdentityContext;
import licode.unisop.provider.info.SiReturnCode;
import licode.unisop.provider.info.SiUrlProviderInfo;

/**
 * 实现Url远程用户认证插件
 *
 * @author licode
 */
public class SiAuthenticationProviderUrl extends SiUrlCallBase implements SiAuthenticationProvider {
    private SiUrlProviderInfo urlInfo;

    public SiAuthenticationProviderUrl(SiRestfulCallUtil callUtil, SiUrlProviderInfo urlInfo) {
        super(callUtil, urlInfo.getUrl());
        this.urlInfo = urlInfo;
    }

    @Override
    public String getName() {
        return null == urlInfo ? null : urlInfo.getName();
    }

    @Override
    public SiReturnCode authenticate(SiIdentityContext context) {
        String result = execute("auth", JSONUtil.toJsonStr(context));
        if (null == result) {
            return SiReturnCode.builder().code(-1).message("校验失败").build();
        } else {
            return JSONUtil.toBean(result, SiReturnCode.class);
        }
    }
}
